package wsepForum.ui.swing.innerPanels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import java.util.concurrent.ExecutionException;

import javax.swing.JOptionPane;

import wsepForum.common.elements.Section;
import wsepForum.domain.exceptions.UnAuthorizedException;
import wsepForum.network.common.GUIAnswer;
import wsepForum.network.common.GUIAnswer.AnswerType;
import wsepForum.network.common.GUIRequest.RequestType;
import wsepForum.persistance.exceptions.WrongElementIDException;
import wsepForum.ui.swing.outerPanels.AOuterPanel;
import wsepForum.ui.swing.workers.ForumWorker;

public class CreateSectionPanel extends AManageSectionPanel {

	private static final long serialVersionUID = -1087794710943888113L;
	
	private int parentSectionID;

	public CreateSectionPanel(AOuterPanel panel, Vector<String> userList, int parentID) {
		super(panel,userList);
		this.parentSectionID = parentID;
		initPanel();
	}

	@Override
	public void addSubmitFunctionality() {
		this.btnSubmit.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ForumWorker<AInnerPanel> worker = new ForumWorker<AInnerPanel>(outerPanel.getConnection()){

					@Override
					protected AInnerPanel doInBackground() throws Exception {
						AInnerPanel returnedPanel = CreateSectionPanel.this;
						String newSecName = sectionNameTextField.getText();
						Vector<String> newSecMods = new Vector<String>();
						for (int selected : allUsersList.getSelectedIndices()) {
							newSecMods.add((String)allUsersList.getModel().getElementAt(selected));
						}
						if(!newSecName.isEmpty()){
							GUIAnswer ans = request(RequestType.CREATE_SECTION, new Object[] {parentSectionID,newSecName,newSecMods});
							outerPanel.setNumOfConnected(ans.getNumOfConnected());
							if(ans.getAnswerType().equals(AnswerType.ANSWER) && ans.getArguments().length == 2){
								returnedPanel = new SectionPanel((Section) ans.getArguments()[0],(Boolean) ans.getArguments()[1],outerPanel);
							}
							else {
								returnedPanel = handleSubmitExceptions((Exception) ans.getArguments()[0]);
							}
						}
						else{
							JOptionPane.showMessageDialog(null, "You must give a section name");	
						}
						return returnedPanel;
					}

					@Override
					public void done(){
						try {
							outerPanel.swapInnerPanel(get());
						} catch (InterruptedException e) {
							outerPanel.swapInnerPanel(handleSubmitExceptions(e));
						} catch (ExecutionException e) {
							outerPanel.swapInnerPanel(handleSubmitExceptions(e));
						}
					}

					private AInnerPanel handleSubmitExceptions(Exception thrown){
						try {
							throw thrown;
						} catch (WrongElementIDException e) {
							JOptionPane.showMessageDialog(null, "the section you are trying to edit was deleted by another user", "Section deleted",JOptionPane.ERROR_MESSAGE);
							return requestHomePanel(outerPanel);
						} catch (UnAuthorizedException e) {
							JOptionPane.showMessageDialog(null, "you are not authorized to do that", "Unauthorized use",JOptionPane.ERROR_MESSAGE);
							return requestHomePanel(outerPanel);
						} catch (Exception e) {
							JOptionPane.showMessageDialog(null, "operation failed!\n" + e.getMessage(), "Error",JOptionPane.ERROR_MESSAGE);
							return CreateSectionPanel.this;
						}
					}
				
				};
				worker.execute();
			}
		});
	}

	@Override
	protected void setDitails() {
		this.allUsersList.setListData(this.usernameList);
	}

}
